package com.sl.springboot.controller;

import com.sl.springboot.model.*;
import com.sl.springboot.service.EvaluationService;
import com.sl.springboot.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description: 控制层
 * @author: mty
 */
@Controller
public class EvaluationController {

    // 依赖注入
    @Autowired
    private EvaluationService evaluationService;

    @Autowired
    private OrdersService ordersService;

    /**
     * 分页查询
     * pageIndex 当前页码
     * pageSize  显示条数
     */
    @RequestMapping(value = "/findEvaluation")
    public String findEvaluation(Integer pageIndex, Integer pageSize ,String cname,String uname, Model model,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "login";
        }
        Map mp = new HashMap();
        mp.put("cname",cname);
        mp.put("uname",uname);
        String type = (String)session.getAttribute("type");
        if(type.equals("02")){
            User u = (User)session.getAttribute("ad");
            mp.put("uid",u.getId());
        }
        PageInfo<Evaluation> pageList = evaluationService.findPageInfo(pageIndex,pageSize,mp);
        model.addAttribute("pageList",pageList);
        return "EvaluationList";
    }


    /**
     * 添加
     */
    @RequestMapping(value = "/addEvaluation" ,method = RequestMethod.POST)
    @ResponseBody
    public String addEvaluation( @RequestBody Evaluation evaluation) {
        int d = evaluationService.addEvaluation(evaluation);
        return "EvaluationList";
    }


    /**
     * 评价
     */
    @RequestMapping(value = "/addEvaluationPost" ,method = RequestMethod.POST)
    @ResponseBody
    public String addEvaluationPost(@RequestBody Evaluation evaluation ,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "201";
        }
        User user = (User) session.getAttribute("ad");
        String oid = evaluation.getId();
        evaluation.setUid(user.getId());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        evaluation.setPubTimne(time);
        int d = evaluationService.addEvaluation(evaluation);
        //更新订单状态
        Orders ordersById = ordersService.findOrdersById(oid);
        ordersById.setStatus("02");
        ordersService.updateOrders(ordersById);
        return "200";
    }



    /**
     * 回复
     */
    @RequestMapping(value = "/addReplyPost" ,method = RequestMethod.POST)
    @ResponseBody
    public String addReplyPost(@RequestBody Evaluation evaluation ,HttpServletRequest request) {
        HttpSession session = request.getSession();
        if(session.getAttribute("ad") == null){
            session.setAttribute("msg", "对不起，请登录！");
            return "201";
        }
        Admin admin = (Admin) session.getAttribute("ad");
        Evaluation evaluationById = evaluationService.findEvaluationById(evaluation.getId());
        evaluationById.setReplyContent(evaluation.getReplyContent());
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = simpleDateFormat.format(date);
        evaluationById.setReplyTime(time);
        evaluationService.updateEvaluation(evaluationById);
        return "200";
    }


    /**
     * 删除
     */
    @RequestMapping( "/deleteEvaluation")
    @ResponseBody
    public String deleteEvaluation(String id) {
        int d = evaluationService.deleteEvaluation(id);
        return "EvaluationList";
    }


    /**
     * 修改
     */
    @RequestMapping( "/updateEvaluation")
    public String updateEvaluation( Evaluation evaluation) {
        int d = evaluationService.updateEvaluation(evaluation);
        return "redirect:/findEvaluation";
    }


    /**
     * 按照ID查询
     */
    @RequestMapping( "/findEvaluationById")
    public String findEvaluationById(String id,Model model,HttpServletRequest request) {
        Evaluation evaluation= evaluationService.findEvaluationById(id);
        model.addAttribute("evaluation",evaluation);
        return "EvaluationEdit";
    }


}
